package app.inmos.functions.json;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.StrictMode;
/**
 *  Clase que se utiliza para obtener los datos generales de JSON, utilizado para cualquier objeto del sistema.
 * @author Lisandro
 *
 */
public class BaseUtilsJSON {
	
	
	
	/**
	 * Se obtiene el String de la estructura correspondiente de la cual se invoca.
	 * @param pUrl
	 * @return String
	 */
	public static String obtenerStringJSONConURL(String pUrl){
		
	   String json=null;
		
	   try{
		   
		   
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
				  
		    StrictMode.setThreadPolicy(policy);

			//TODO Esta url se debe leer de un fuente externo  
			HttpGet httpGet = new HttpGet(pUrl);
			HttpClient httpClient = new DefaultHttpClient();
		    HttpResponse response = (HttpResponse)httpClient.execute(httpGet);
		    HttpEntity entity = response.getEntity();
		    BufferedHttpEntity buffer = new BufferedHttpEntity(entity);
		    InputStream iStream = buffer.getContent();
		                     
		    BufferedReader reader = new BufferedReader(new InputStreamReader(iStream, "iso-8859-1"), 8);
	        StringBuilder sb = new StringBuilder();
	        String line = null;
		    while ((line = reader.readLine()) != null) {
		    	sb.append(line + "\n");
		    }
		    
		    iStream.close();
		    json = sb.toString();
	    }
	   
	 
		catch(Exception e){
			  System.out.println("Error " + e.getMessage());
		}
	   
        return json;
	}

}
